package cn.iocoder.yudao.module.train.dal.mysql.appointment;

import java.util.*;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.module.train.dal.dataobject.appointment.AppointmentDO;
import cn.iocoder.yudao.module.train.dal.dataobject.connectDTO.AppointmentDTO;
import cn.iocoder.yudao.module.train.dal.dataobject.connectDTO.CourseClassesCommodityDTO;
import cn.iocoder.yudao.module.train.dal.dataobject.courseclasses.CourseClassesDO;
import cn.iocoder.yudao.module.train.dal.dataobject.coursecommodity.CourseCommodityDO;
import cn.iocoder.yudao.module.train.dal.dataobject.student.StudentDO;
import cn.iocoder.yudao.module.train.dal.dataobject.teacher.TeacherDO;
import cn.iocoder.yudao.module.train.dal.dataobject.venue.VenueDO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.apache.ibatis.annotations.Mapper;
import cn.iocoder.yudao.module.train.controller.admin.appointment.vo.*;

/**
 * 预约 Mapper
 *
 * @author waynelam
 */
@Mapper
public interface AppointmentMapper extends BaseMapperX<AppointmentDO> {
    default PageResult<AppointmentDTO> selectPage(AppointmentPageReqVO reqVO) {
        Page<AppointmentDTO> page = new Page<>(reqVO.getPageNo(), reqVO.getPageSize());
        MPJLambdaWrapper<AppointmentDO> wrapper = new MPJLambdaWrapper<AppointmentDO>()
                .selectAll(AppointmentDO.class)
                .select(StudentDO::getStudentName)
                .leftJoin(StudentDO.class, StudentDO::getId, AppointmentDO::getStudentId)
                .select(CourseCommodityDO::getCommodityName)
                .leftJoin(CourseCommodityDO.class, CourseCommodityDO::getId, AppointmentDO::getCommodityId)
                .select(TeacherDO::getTeacherName)
                .leftJoin(TeacherDO.class, TeacherDO::getId, CourseCommodityDO::getTeacherId)
                .select(VenueDO::getVenueNo)
                .leftJoin(VenueDO.class, VenueDO::getId, AppointmentDO::getVenueId)
                .likeIfExists(StudentDO::getStudentName, reqVO.getStudentName())
                .likeIfExists(TeacherDO::getTeacherName, reqVO.getTeacherName())
                .likeIfExists(VenueDO::getVenueNo, reqVO.getVenueNo())
                .likeIfExists(CourseCommodityDO::getCommodityName, reqVO.getCommodityName())
                .orderByDesc("create_time");
        if (reqVO.getAppointmentDate() != null) {
            wrapper.between(AppointmentDO::getAppointmentDate, reqVO.getAppointmentDate()[0], reqVO.getAppointmentDate()[1]);
        }
        if (reqVO.getCreateTime() != null) {
            wrapper.between(AppointmentDO::getCreateTime, reqVO.getCreateTime()[0], reqVO.getCreateTime()[1]);
        }
        IPage<AppointmentDTO> resultPage = selectJoinPage(page, AppointmentDTO.class, wrapper);
        return new PageResult<>(resultPage.getRecords(), resultPage.getTotal());
    }

    default List<AppointmentDTO> selectList(AppointmentPageReqVO reqVO) {
        MPJLambdaWrapper<AppointmentDO> wrapper = new MPJLambdaWrapper<AppointmentDO>()
                .selectAll(AppointmentDO.class)
                .select(StudentDO::getStudentName)
                .leftJoin(StudentDO.class, StudentDO::getId, AppointmentDO::getStudentId)
                .select(CourseCommodityDO::getCommodityName)
                .leftJoin(CourseCommodityDO.class, CourseCommodityDO::getId, AppointmentDO::getCommodityId)
                .select(TeacherDO::getTeacherName)
                .leftJoin(TeacherDO.class, TeacherDO::getId, CourseCommodityDO::getTeacherId)
                .select(VenueDO::getVenueNo)
                .leftJoin(VenueDO.class, VenueDO::getId, AppointmentDO::getVenueId)
                .eqIfExists(StudentDO::getId, reqVO.getStudentId())
                .eqIfExists(CourseCommodityDO::getId, reqVO.getCommodityId())
                .likeIfExists(StudentDO::getStudentName, reqVO.getStudentName())
                .likeIfExists(TeacherDO::getTeacherName, reqVO.getTeacherName())
                .likeIfExists(VenueDO::getVenueNo, reqVO.getVenueNo())
                .likeIfExists(CourseCommodityDO::getCommodityName, reqVO.getCommodityName())
                .orderByDesc("create_time");
        if (reqVO.getAppointmentDate() != null) {
            wrapper.between(AppointmentDO::getAppointmentDate, reqVO.getAppointmentDate()[0], reqVO.getAppointmentDate()[1]);
        }
        if (reqVO.getCreateTime() != null) {
            wrapper.between(AppointmentDO::getCreateTime, reqVO.getCreateTime()[0], reqVO.getCreateTime()[1]);
        }
        return selectJoinList(AppointmentDTO.class, wrapper);
    }

}
